package com.luoxiang.socialandroid.activity;

import static com.luoxiang.lib_common.common.Constants.MmkvConst.EXTRA;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.KEY;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.TOKEN;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.adapter.MyOperationsAdapter;
import com.luoxiang.socialandroid.base.BaseActivity;
import com.luoxiang.socialandroid.bean.MyOpBean;
import com.luoxiang.socialandroid.bean.NumberBean;
import com.luoxiang.socialandroid.utils.AESUtil;
import com.luoxiang.socialandroid.utils.MmkvUtil;
import com.luoxiang.socialandroid.utils.NetUtils2;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MyOperationsActivity extends BaseActivity {
    private ImageView icon_back;
    private String token;
    private RecyclerView rc_myop;
    private MyOperationsAdapter adapter;
    private int intentflag;

    @Override
    protected void initListener() {
        icon_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        setContentView(R.layout.activity_myop);
        setStatusBarTranslucent();
    }

    @Override
    protected void initView() {
        showLoading();
        Intent intent = getIntent();
        intentflag = intent.getIntExtra("intentflag", -1);

        icon_back = findViewById(R.id.icon_back);
        rc_myop = findViewById(R.id.rc_myop);
        token = MmkvUtil.INSTANCE.decodeString(TOKEN);
        getMyOperations(token, EXTRA, 1, 100);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getMyOperations(token, EXTRA, 1, 10);
    }

    private void getMyOperations(String token, String extra, int i, int i1) {
        NetUtils2.getInstance().getApiService().getUserDynaimcs(token, extra, i, i1)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        clearLoading();
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        try {
                            LogUtils.i("decryptStrop", decryptStr);
                            Gson gson = new Gson();
                            MyOpBean bean = gson.fromJson(decryptStr, MyOpBean.class);

                            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MyOperationsActivity.this);
                            rc_myop.setLayoutManager(linearLayoutManager);
                            if (bean.getData() != null) {
                                if (bean != null) {
                                    adapter = new MyOperationsAdapter(bean.getData().getDynamics(), intentflag);
                                }
                            }
                            rc_myop.setAdapter(adapter);
                        } catch (Exception e) {
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        clearLoading();
                    }

                    @Override
                    public void onComplete() {
                        clearLoading();
                    }
                });
    }
}
